package com.dd.keel.core.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;   
import java.io.ByteArrayOutputStream;   
import java.io.IOException;   
import java.io.ObjectInputStream;   
import java.io.ObjectOutputStream;   
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;   
import java.util.zip.ZipInputStream;   
import java.util.zip.ZipOutputStream;   

/**
 * Compress util
 * @author Kevin.XU
 *
 */
public class ZipUtil {
	
	final static private Logger LOGGER = LoggerFactory.getLogger(ZipUtil.class);
	
	public static byte[] compress(byte[] byteArray)
	{
		if(byteArray==null) return null;
	    byte[] compressed;
		ByteArrayOutputStream out = null;
		ZipOutputStream zout = null;
		try {
			out = new ByteArrayOutputStream();
			zout = new ZipOutputStream(out);
			zout.putNextEntry(new ZipEntry("0"));
			zout.write(byteArray);
			zout.closeEntry();
			compressed = out.toByteArray();
		} catch (IOException ex) {
			LOGGER.error( ex.getMessage(), ex );
			compressed = null;
		} finally {
			if (zout != null) {
				try {
					zout.close();
				} catch (IOException e) {
					LOGGER.error( e.getMessage(), e );
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					LOGGER.error( e.getMessage(), e );
				}
			}
		}   
		return compressed;
	}
	
	public static byte[] decompress(byte[] compressed)
	{
		if(compressed==null) return null;
		byte[] decompressed;
		ByteArrayOutputStream out = null;
		ByteArrayInputStream in = null;
		ZipInputStream zin = null;
		try {
			out = new ByteArrayOutputStream();
			in = new ByteArrayInputStream(compressed);
			zin = new ZipInputStream(in);
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = zin.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressed = out.toByteArray();
		} catch (Exception ex) {
			LOGGER.error( ex.getMessage(), ex );
			decompressed = null;
		} finally {
			if (zin != null) {
				try {
					zin.close();
				} catch (IOException e) {
					LOGGER.error( e.getMessage(), e );
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					LOGGER.error( e.getMessage(), e );
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					LOGGER.error( e.getMessage(), e );
				}
			}
		}
		return decompressed;
	}
	
	public static byte[] gzip_decompress(byte[] compressed)
	{
		if(compressed==null) return null;
		byte[] decompressed;
		ByteArrayOutputStream out = null;
		ByteArrayInputStream in = null;
		GZIPInputStream zin = null;
		try {
			out = new ByteArrayOutputStream();
			in = new ByteArrayInputStream(compressed);
			zin = new GZIPInputStream(in);
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = zin.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressed = out.toByteArray();
		} catch (Exception ex) {
			LOGGER.error( ex.getMessage(), ex );
			decompressed = null;
		} finally {
			if (zin != null) {
				try {
					zin.close();
				} catch (IOException e) {
					LOGGER.error( e.getMessage(), e );
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					LOGGER.error( e.getMessage(), e );
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					LOGGER.error( e.getMessage(), e );
				}
			}
		}
		return decompressed;
	}
	
}
